package com.qf.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.http.Result;
import com.qf.entity.dto.AdminModuleRole;
import com.qf.entity.dto.AdminRole;
import com.qf.entity.po.LoginAdmin;
import com.qf.service.impl.AdminModuleRoleService;
import com.qf.service.impl.AdminModuleService;
import com.qf.service.impl.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("permission")
public class PermissionController {

    @Resource
    AdminRoleService adminRoleService;

    @Resource
    AdminModuleService adminModuleService;

    @Autowired
    private AdminModuleRoleService adminModuleRoleService;

    @RequestMapping("list")
    public String toList(Model model, LoginAdmin loginAdmin){
        QueryWrapper<AdminRole> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(AdminRole::getOrderNum);
        model.addAttribute("roleList",adminRoleService.list(wrapper));
        model.addAttribute("moduleList",adminModuleService.getModuleById(loginAdmin.getId()));
        return "permission/list";
    }


    public class RoleInfo {
        private AdminRole adminRole;
        private List<AdminModuleRole> adminModuleRoleList;

        public RoleInfo() {
        }

        public RoleInfo(AdminRole adminRole, List<AdminModuleRole> adminModuleRoleList) {
            this.adminRole = adminRole;
            this.adminModuleRoleList = adminModuleRoleList;
        }

        public AdminRole getAdminRole() {
            return adminRole;
        }

        public void setAdminRole(AdminRole adminRole) {
            this.adminRole = adminRole;
        }

        public List<AdminModuleRole> getAdminModuleRoleList() {
            return adminModuleRoleList;
        }

        public void setAdminModuleRoleList(List<AdminModuleRole> adminModuleRoleList) {
            this.adminModuleRoleList = adminModuleRoleList;
        }
    }

    @PostMapping("getById")
    @ResponseBody
    public Result<?> getById(@RequestParam(value = "id", required = true) Long id) {
        AdminRole adminRole = adminRoleService.getById(id); //前端选择的角色的id
        List<AdminModuleRole> adminModuleRoleList = adminModuleRoleService.getByRoleId(id); //前端选择的角色的id的权限
        return Result.success(new RoleInfo(adminRole, adminModuleRoleList));
    }

    @PostMapping("save")
    @ResponseBody
    public Result<?> save(AdminRole adminRole, @RequestParam("moduleRoles") String moduleRoles) {
        adminRoleService.saveRole(adminRole, moduleRoles);
        return Result.success();
    }

    @PostMapping("del")
    @ResponseBody
    public Result<?> del(@RequestParam(value = "id", required = true) Long id) {
        adminRoleService.removeById(id);
        return Result.success();
    }

}
